<?
include("class/Member.php");
include("class/SoapInit.class.php");
include("class/Bill.php");
include("class/BillPay.php");//现场支付/在线支付账单
//$ac=$GLOBALS['_ac'];
//$pw=$GLOBALS['_pw'];
$s = SoapInit::GetSoapInside();
$memberID=$_SESSION['sid'];
$m=new Member($s,$memberID);
$m_info=$m->info();
$statusT=array("0"=>"<em class='red'>未结算</em>",1=>"<em class='orange'>结算中</em>",2=>"<em class='green'>已结算</em>");



if($method==1){// 应收
	/*自动结算配置******************/
	/*分销商结算配置*/
	$fids           = $m->distributors();                    //分销商
	$settlefids     = $m->settlefids();                    //已配置分销商
	$unsettlefids   = array_diff($fids, $settlefids);//未配置分销商
	$unfids         = array_diff($settlefids, $fids);      //解除分销关系的分销商
	//已解除分销关系商家，停用结算配置
	if(count($unfids)!=0){
		$unfids = implode(",",$unfids);
		$str    = "UPDATE uu_settle SET status=1 WHERE aid=$memberID AND fid IN ($unfids)";
		$GLOBALS['le']->query($str);
	}
	//未配置分销商，自动插入配置，按月结
	if(count($unsettlefids)!=0){
		$fid_str = implode(',', $unsettlefids);
		$str="SELECT id,creattime FROM pft_member WHERE id IN($fid_str)";
		$GLOBALS['le']->query($str);
		while($row=$GLOBALS['le']->fetch_assoc()) {
			$fid_create[$row['id']] = $row['createtime'];
		}
		$str="SELECT id,fid FROM uu_settle WHERE aid=$memberID AND fid IN($fid_str)";
		$GLOBALS['le']->query($str);
		$insert_str = '';
		while($row=$GLOBALS['le']->fetch_assoc()) {
			if (!in_array($row['fid'], $unsettlefids)) {
				$insert_str .= "({$row['fid']},$memberID,31, {$fid_create[$row['fid']]}),";
			}
		}
		if (!empty($insert_str)) {
			$insert = "INSERT uu_settle (fid,aid,billdate,begintime) VALUES"
					. rtrim($insert_str, ',');
			$GLOBALS['le']->query($insert);
		}
	}
	//分销关系的分销商，结算配置需有效
	if(count($fids)!=0){
		$str="UPDATE uu_settle SET status=0 WHERE aid=$memberID AND fid IN (".implode(",",$fids).")";
		$GLOBALS['le']->query($str);
	}
	/*按结算配置自动生成应收款账单*/
	$gets=array();
	$str="SELECT fid FROM uu_settle WHERE aid=$memberID and status=0";
	$GLOBALS['le']->query($str);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$gets[]=$row['fid'];
	}
	foreach($gets as $id){
		// echo $id;
		$b=new Bill($id,$memberID,$s,$GLOBALS['le']);
		$b->getbill();
	}


}else{// 应付
	/*供应商结算配置*/
	$aids       =$m->appiers();                            //获取供应商
	$settleaids =$m->settleaids();                    //已配置供应商
	$unsettleaids=array_diff($aids,$settleaids);    //未配置供应商
	$unaids=array_diff($settleaids,$aids);            //解除分销关系的分销商
	//已解除供销关系商家，停用结算配置
	if(count($unaids)!=0){
		$unaids=implode(",",$unaids);
		$str="update uu_settle set status=1 where aid in ($unaids) and fid=$memberID";
		$GLOBALS['le']->query($str);
	}
	//未配置分销商，自动插入配置，按月结
	if(count($unsettleaids)!=0){
		$creattime=$m_info['creattime'];

		$aid_str = implode(',', $unsettleaids);

		$str="SELECT id,aid FROM uu_settle WHERE fid=$memberID AND aid IN($aid_str)";
		$GLOBALS['le']->query($str);
		$insert_str = '';
		while($row=$GLOBALS['le']->fetch_assoc()) {
			if (!in_array($row['aid'], $unsettleaids)) {
				$insert_str .= "($memberID,{$row['aid']},31,{$creattime}),";
			}
		}
		if (!empty($insert_str)) {
			$insert = "INSERT uu_settle (fid,aid,billdate,begintime) VALUES"
				. rtrim($insert_str, ',');
			$GLOBALS['le']->query($insert);
		}
	}
	//供销关系的供应商，结算配置需有效
	if(count($aids)!=0){
		$str="UPDATE uu_settle SET status=0 WHERE fid=$memberID AND aid in (".implode(",",$aids).")";
		$GLOBALS['le']->query($str);
	}
	/*结算配置*end*****************/
	/*按结算配置自动生成应付款账单*/
	$pays=array();
	$str="SELECT aid FROM uu_settle WHERE fid=$memberID AND status=0";
	$GLOBALS['le']->query($str);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$pays[]=$row['aid'];
	}
	foreach($pays as $id){
		$b=new Bill($memberID,$id,$s,$GLOBALS['le']);
		$b->getbill();
	}

}




$showOfflineFlag = false;
$accountLimitList = array(123950,123951,123952,123953,123954,123955,3348);//云顶供应商账号
$accountLimitList[] = 100014;//测试账号
$accountLimitList[] = 200566;//测试账号
$host = $_SERVER['HTTP_HOST'];
if($_SERVER['HTTP_HOST']=='yyd.12301.cc' || in_array( $_SESSION['saccount'],$accountLimitList)) {
    $showOfflineFlag = true;
}

if ($showOfflineFlag) {
	if($method==1){
		foreach($gets as $id){
			$billOffline = new BillPay($id,$memberID,$s,$GLOBALS['le'], BillPay::PAY_OFFLINE);
			$billOffline->getbill();
			$billOnline = new BillPay($id,$memberID,$s,$GLOBALS['le'], BillPay::PAY_ONLINE);
			$billOnline->getbill();
		}
	}else{
		foreach($pays as $id){
			$billOffline = new BillPay($memberID,$id,$s,$GLOBALS['le'], BillPay::PAY_OFFLINE);
			$billOffline->getbill();
			$billOnline = new BillPay($memberID,$id,$s,$GLOBALS['le'], BillPay::PAY_ONLINE);
			$billOnline->getbill();
		}
	}


}

$dnames = include '/var/www/html/new/d/module/link_product/Member.cache.php';
?>